package com.example.app.controller.user;

import com.example.app.domain.user.UserInfoVo;
import com.example.app.domain.user.UserLoginInfoVo;
import com.example.module.module.user.entity.User;
import com.example.module.module.user.service.UserService;
import com.example.module.utils.Response;
import com.example.module.utils.SignUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/app")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Response register(@RequestParam String phone,
                             @RequestParam String password,
                             @RequestParam String name,
                             @RequestParam String avatar) {
        try {
            userService.register(phone, password, name, avatar);
            return new Response<>(1001,"注册成功");
        } catch (Exception e) {
            return new Response<>(4004,e.getMessage());
        }
    }

    @PostMapping("/login")
    public Response login(@RequestParam(name = "phone")String phone,
                          @RequestParam(name = "password") String password) {
        User user = userService.login(phone, password);

        UserInfoVo userInfo = new UserInfoVo();
        userInfo.setName(user.getName());
        userInfo.setPhone(user.getPhone());
        userInfo.setAvatar(user.getAvatar());
        userInfo.setUserId(user.getId());

        UserLoginInfoVo loginInfo = new UserLoginInfoVo();
        loginInfo.setSign(SignUtils.makeSign(user.getId()));

        loginInfo.setUserInfo(userInfo);

    return new Response(1001,loginInfo);
    }
}
